package forms;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;

import model.UserModel;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import db.DbException;

public class UserForm extends ActionForm {
	
	// ============== Debug Flag ================= //
	private static final boolean DEBUG = true;
	
	// =============== Form Variables ============= //
	private String username = null;
	private String password = null;
	private String password_retype = null;
	private String email = null;
	
	// =============== Getter and Setter =========== //
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword_retype() {
		return password_retype;
	}
	public void setPassword_retype(String password_retype) {
		this.password_retype = password_retype;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	/**
	 * reset
	 * 
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.setUsername(null);
		this.setPassword(null);
		this.setPassword_retype(null);
		this.setEmail(null);
	}

	/**
	 * ActionErrors
	 * 
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
	
		ActionErrors errors = new ActionErrors();
		
		// Check username
		if (username == null || username.length() < 1) {
			errors.add("usernameMsgTag1", new ActionMessage("errors.required",
			"User Name"));
		}
		else {
			try {
				RowSet rset = UserModel.getUser(username);
				
				int user_id = 0;
				
				try {
					while (rset.next()) {
						user_id = rset.getInt("id");
					}
				} catch (SQLException ex) {
					System.err.println("DbException: " + ex.getMessage());
				}
				
				if (user_id > 0) {
					errors.add("userMsgTag2", new ActionMessage("error.reg.user",
					username));
				}
				
			} catch (DbException ex) {
				System.err.println("DbException: " + ex.getMessage());
			}
		}
		
		// Check password
		if (password == null || password.length() < 1) {
			errors.add("passwordMsgTag1", new ActionMessage("errors.required",
			"Password"));
		}
		
		// Check email
		if (email == null || email.length() < 1) {
			errors.add("emailMsgTag1", new ActionMessage("errors.required",
			"Email"));
		}
		else {
			try {
				RowSet rset = UserModel.getUserbyEmail(email);
				
				int user_id = 0;
				
				try {
					while (rset.next()) {
						user_id = rset.getInt("id");
					}
				} catch (SQLException ex) {
					System.err.println("DbException: " + ex.getMessage());
				}
				
				if (user_id > 0) {
					errors.add("emailMsgTag2", new ActionMessage("error.reg.email",
					email));
				}
				
			} catch (DbException ex) {
				System.err.println("DbException: " + ex.getMessage());
			}
		}
		
		return errors;
	}
}
